#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/08/20 3:56 下午
# @Author : liuchong
# @Software: PyCharm
# @File: sms_code.py

import random

import ujson
from sanic import Blueprint
from sanic.request import json_loads
from sanic.response import json
from config.code_config import RES_CODE_200, RES_CODE_400
from services.msg_send import send_sms
# from lib.redisdbs import RedisClient


account_bp = Blueprint('account', url_prefix='account')


@account_bp.route('/send_code', methods=[ 'GET', 'POST' ], name='send_code')
async def send_code(request):
    """
    发送验证码
    :param request:
    :return:
    """
    data = json_loads(request.body)
    mobile = data.get('mobile', '')
    sms_code = random.randint(1110, 9998)
    text = sms_code
    res = send_sms(text, phone=mobile)
    res_dict = ujson.loads(res)
    if res_dict.get('code') == '0':
        return json({'code': RES_CODE_200, 'info': '发送成功'})
    else:
        return json({'code': RES_CODE_400, 'msg': print(send_sms(text, mobile)), 'info': '发送失败'})
